package com.stylefeng.guns.cinema.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.cinema.CinemaServiceAPI;
import com.stylefeng.guns.api.cinema.vo.*;
import com.stylefeng.guns.api.order.vo.OrderQueryVO;
import com.stylefeng.guns.cinema.dao.*;
import com.stylefeng.guns.cinema.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dengwubo
 * executes 线程数量控制
 */
@Service(executes = 10)
@Component
public class DefaultCinemaServiceImpl implements CinemaServiceAPI {
    @Autowired
    private MoocCinemaTMapper cinemaTMapper;
    @Autowired
    private MoocAreaDictTMapper areaDictTMapper;
    @Autowired
    private MoocBrandDictTMapper brandDictTMapper;
    @Autowired
    private MoocHallDictTMapper hallDictTMapper;
    @Autowired
    private MoocHallFilmInfoTMapper hallFilmInfoTMapper;
    @Autowired
    private MoocFieldTMapper fieldTMapper;

    /**
     * 根据CinemaQueryVO，查询影院列表
     *
     * @param cinemaQueryVO
     * @return
     */
    @Override
    public Page<CinemaVO> getCinemas(CinemaQueryVO cinemaQueryVO) {
        // 业务实体集合
        List<CinemaVO> cinemas = new ArrayList<>();

        Page<MoocCinemaT> page = new Page<>(cinemaQueryVO.getNowPage(), cinemaQueryVO.getPageSize());
        EntityWrapper<MoocCinemaT> wrapper = new EntityWrapper();
        if (cinemaQueryVO.getBrandId() != 99) {
            wrapper.eq("brand_id", cinemaQueryVO.getBrandId());
        }
        if (cinemaQueryVO.getDistrictId() != 99) {
            wrapper.eq("area_id", cinemaQueryVO.getDistrictId());
        }
        if (cinemaQueryVO.getHallType() != 99) {
            wrapper.like("hall_ids", "%#" + cinemaQueryVO.getHallType() + "#%");
        }
        List<MoocCinemaT> moocCinemaTS = cinemaTMapper.selectPage(page, wrapper);
        for (MoocCinemaT moocCinemaT : moocCinemaTS) {
            CinemaVO cinemaVO = new CinemaVO();
            cinemaVO.setUuid(moocCinemaT.getUuid() + "");
            cinemaVO.setAddress(moocCinemaT.getCinemaAddress());
            cinemaVO.setCinemaName(moocCinemaT.getCinemaName());
            cinemaVO.setMinimumPrice(moocCinemaT.getMinimumPrice() + "");
            cinemas.add(cinemaVO);
        }
        long counts = cinemaTMapper.selectCount(wrapper);
        Page<CinemaVO> result = new Page<>();
        result.setRecords(cinemas);
        result.setSize(cinemaQueryVO.getPageSize());
        result.setTotal(counts);
        return result;
    }

    /**
     * 根据条件获取品牌列表
     *
     * @param brandId
     * @return
     */
    @Override
    public List<BrandVO> getBrands(int brandId) {
        List<BrandVO> brandVOS = new ArrayList<>();
        boolean flag = false;
        MoocBrandDictT moocBrandDictT = brandDictTMapper.selectById(brandId);
        if (brandId == 99 || moocBrandDictT == null || moocBrandDictT.getUuid() == null) {
            flag = true;
        }
        List<MoocBrandDictT> moocBrandDictTS = brandDictTMapper.selectList(null);
        for (MoocBrandDictT brand : moocBrandDictTS) {
            BrandVO brandVO = new BrandVO();
            brandVO.setBrandName(brand.getShowName());
            brandVO.setBrandId(brand.getUuid() + "");
            if (flag) {
                if (brand.getUuid() == 99) {
                    brandVO.setActive(true);
                }
            } else {
                if (brandId == brand.getUuid()) {
                    brandVO.setActive(true);
                }
            }
            brandVOS.add(brandVO);
        }
        return brandVOS;
    }

    /**
     * 获取行政区域列表
     *
     * @param areaId
     * @return
     */
    @Override
    public List<AreaVO> getAreas(int areaId) {
        List<AreaVO> areaVOS = new ArrayList<>();
        boolean flag = false;
        MoocAreaDictT moocAreaDictT = areaDictTMapper.selectById(areaId);
        if (areaId == 99 || moocAreaDictT == null || moocAreaDictT.getUuid() == null) {
            flag = true;
        }

        List<MoocAreaDictT> moocAreaDictTS = areaDictTMapper.selectList(null);
        for (MoocAreaDictT area : moocAreaDictTS) {

            AreaVO areaVO = new AreaVO();
            areaVO.setAreaName(area.getShowName());
            areaVO.setAreaId(area.getUuid() + "");
            if (flag) {
                if (area.getUuid() == 99) {
                    areaVO.setActive(true);
                }
            } else {
                if (areaId == area.getUuid()) {
                    areaVO.setActive(true);
                }
            }
            areaVOS.add(areaVO);
        }
        return areaVOS;
    }

    /**
     * 获取影厅类型列表
     *
     * @param hallTypeId
     * @return
     */
    @Override
    public List<HallTypeVO> getHallTypes(int hallTypeId) {
        List<HallTypeVO> hallTypeVOS = new ArrayList<>();
        boolean flag = false;
        MoocHallDictT moocHallDictT = hallDictTMapper.selectById(hallTypeId);
        if (hallTypeId == 99 || moocHallDictT == null || moocHallDictT.getUuid() == null) {
            flag = true;
        }

        List<MoocHallDictT> moocHallDictTS = hallDictTMapper.selectList(null);
        for (MoocHallDictT hallType : moocHallDictTS) {

            HallTypeVO hallTypeVO = new HallTypeVO();
            hallTypeVO.setHalltypeName(hallType.getShowName());
            hallTypeVO.setHalltypeId(hallType.getUuid() + "");
            if (flag) {
                if (hallType.getUuid() == 99) {
                    hallTypeVO.setActive(true);
                }
            } else {
                if (hallTypeId == hallType.getUuid()) {
                    hallTypeVO.setActive(true);
                }
            }
            hallTypeVOS.add(hallTypeVO);
        }
        return hallTypeVOS;
    }

    /**
     * 根据影院编号，获取影院信息
     *
     * @param cinemaId
     * @return
     */
    @Override
    public CinemaInfoVO getCinemaInfoById(int cinemaId) {

        MoocCinemaT moocCinemaT = cinemaTMapper.selectById(cinemaId);
        CinemaInfoVO cinemaInfoVO = new CinemaInfoVO();
        cinemaInfoVO.setImgUrl(moocCinemaT.getImgAddress());
        cinemaInfoVO.setCinemaPhone(moocCinemaT.getCinemaPhone());
        cinemaInfoVO.setCinemaName(moocCinemaT.getCinemaName());
        cinemaInfoVO.setCinemaId(moocCinemaT.getUuid()+"");
        cinemaInfoVO.setCinemaAddress(moocCinemaT.getCinemaAddress());
        return cinemaInfoVO;
    }

    /**
     * 获取所有电影的信息和对应的放映场次信息，根据影院编号
     *
     * @param cinemaId
     * @return
     */
    @Override
    public List<FilmInfoVO> getfilmInfoByCinemaId(int cinemaId) {
        List<FilmInfoVO> filmInfos = fieldTMapper.getFilmInfos(cinemaId);

        return filmInfos;
    }

    /**
     * 根据放映场次ID获取放映信息
     *
     * @param fieldId
     * @return
     */
    @Override
    public HallInfoVO getFilmFieldInfo(int fieldId) {
        HallInfoVO hallInfo = fieldTMapper.getHallInfo(fieldId);
        return hallInfo;
    }

    /**
     * 据放映场次查询播放的电影编号，然后根据电影编号获取对应的电影信息
     *
     * @param fieldId
     * @return
     */
    @Override
    public FilmInfoVO getFilmInfoByFieldId(int fieldId) {
        FilmInfoVO filmInfoById = fieldTMapper.getFilmInfoById(fieldId);
        return filmInfoById;
    }

    /**
     * 该部分是订单模块需要的内容
     *
     * @param fieldId
     * @return
     */
    @Override
    public OrderQueryVO getOrderNeeds(int fieldId) {
        OrderQueryVO orderQueryVO = new OrderQueryVO();
        MoocFieldT moocFieldT = fieldTMapper.selectById(fieldId);
        orderQueryVO.setCinemaId(moocFieldT.getCinemaId()+"");
        orderQueryVO.setFilmPrice(moocFieldT.getPrice()+"");
        return orderQueryVO;
    }
}
